<!--
 * @Author: 赵克强 huohuo625@163.com
 * @Date: 2023-11-10 15:01:12
 * @LastEditors: 赵克强 huohuo625@163.com
 * @LastEditTime: 2025-01-15 13:57:35
 * @FilePath: \variant-form\src\components\form-designer\form-widget\field-widget\userModel.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog title="用户选择" :visible.sync="open" width="70%" :before-close="close" custom-class="userModel"
    :append-to-body="true">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24" style="border-right: 1px solid #f0f0f0">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="10" :xs="24">
        <el-table v-loading="loading" :data="userList" border size="mini" ref="tableRef"
          @selection-change="handleSelectionChange" :row-key="'userId'">
          <el-table-column type="selection" :reserve-selection="true" width="55" align="center"
            v-if="chooseType === 'YES'">
          </el-table-column>
          <el-table-column label=" " align="center" width="60" v-else>
            <template slot-scope="scope">
              <el-radio v-model="radio" :label="scope.row.userId"
                @change.native="handleSelectionChange(scope.row)">&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="姓名" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName" :show-overflow-tooltip="true" />
        </el-table>

        <div style="display: flex; justify-content: flex-end">
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
      <el-col :span="10" :xs="24">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <span>已选</span>
          </div>
          <!--流程处理表单模块-->
          <el-col :span="24" style="margin-bottom: 20px">
            <el-empty description="暂无数据" v-show="!selectionArr.length"></el-empty>

            <div v-if="selectionArr.length">
              <div v-for="(item, index) in selectionArr" :key="item.id" style="
                  display: flex;
                  justify-content: space-between;
                  border: 1px solid #d9d9d9;
                  align-items: center;
                  padding: 5px 20px;
                  margin-top: 15px;
                  border-radius: 6px;
                ">
                <div>
                  <span>{{ index + 1 }}</span>
                  <span style="margin-left: 10px">{{ item.nickName }}
                    <span style="margin-left: 5px">[ {{ item.userName }} ]</span></span>
                </div>
                <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(item.userId)" style="
                    height: 26px;
                    width: 26px;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                  "></el-button>
              </div>
            </div>
          </el-col>
        </el-card>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end">
      <div>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="handleUserOrDept(selectionArr)">确 定</el-button>
      </div>
    </span>
  </el-dialog>
</template>

<script>
import { deptTreeSelect, listUser } from "@/api/system/user.js";
import pagination from "@/components/Pagination/index";
export default {
  props: {
    open: {
      type: Boolean,
      default: false,
    },
    close: {
      type: Function,
      default: () => { },
    },
    handleUserOrDept: {
      type: Function,
      default: () => { },
    },
    fieldModel: {
      type: Object,
      default: () => { },
    },
    chooseType: {
      type: String,
      default: '',
    },
  },
  components: {
    pagination,
  },
  data() {
    return {
      selectionArr: [],
      loading: false,
      userList: [],
      total: 0,
      deptName: undefined,
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: "0",
        deptId: undefined,
      },
      radio: undefined,
    };
  },
  watch: {
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    handleSelectionChange(arr) {
      if (this.chooseType !== "YES") {
        this.selectionArr = [arr];
        return;
      }
      this.selectionArr = arr;
    },
    handleDelete(id) {
      const row = this.selectionArr.filter((item) => item.userId === id);
      this.selectionArr = this.selectionArr.filter(
        (item) => item.userId !== id
      );
      this.$nextTick(() => {
        this.$refs.tableRef.toggleRowSelection(row?.[0], false);
      });
    },
    getList() {
      this.loading = true;

      listUser({ ...this.queryParams, userType: 'front_user' }).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        const ids = this.fieldModel?.userId?.split(",");
        this.$nextTick(() => {
          this.userList?.forEach((item) => {
            if (ids?.includes(String(item.userId))) {
              if (this.chooseType !== "YES") {
                this.radio = item.userId
                this.selectionArr = [item]
              } else {
                this.$nextTick(() => {
                  this.$refs.tableRef.toggleRowSelection(item, true);
                });
                // this.$refs.tableRef.toggleRowSelection(item, true);
              }

            }
          });
        });
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-row {
  border: none !important;
}

::v-deep .el-radio__label {
  display: none;
}

::v-deep .el-col {
  height: 400px;
  overflow-y: scroll;
}
</style>